<?php
 
namespace app\common\job;
 
use Yii;
use yii\base\BaseObject;
use app\helpers\DatetimeHelper;
use yii\helpers\ArrayHelper;
use app\modules\notify\service\NotifyService;
use app\modules\notify\service\NotifyRangeService;
use app\modules\notify\service\NotifyLatestService;
use app\modules\notify\service\NotifyRangeLatestService;
use app\modules\notify\service\RemindInfoService;
use app\modules\inspect\service\InspectInfoService;
use \yii\queue\Job;
 
class NotifyCommonUpdateJob extends BaseObject implements Job
{
    public $inputs;
    public $userId;
    public $updateTimestamp;
    public $rangeCount;

    public function execute($queue){
        $inputs = $this->inputs;
        $updateTimestamp = $this->updateTimestamp;
        $userId = $this->userId;
        $rangeCount = $this->rangeCount;
        
        //添加通知范围
        $rangeList = [];
        foreach($inputs['range_ids'] as $key => $userId)
        {
            $rangeList[$key]['notify_id'] = $inputs['notify_id'];
            $rangeList[$key]['userid'] = $userId;
            $rangeList[$key]['update_timestamp'] = $updateTimestamp;
            $rangeList[$key]['status'] = 1;
            $rangeList[$key]['is_read'] = 0;
        }
        (new NotifyRangeService)->updateAll(['status' => 0], ['notify_id' => $inputs['notify_id']]);
        (new NotifyRangeService)->saveMany($rangeList, ['notify_id', 'userid']);
        
        (new NotifyRangeLatestService)->updateAll(['status' => 0], ['notify_id' => $inputs['notify_id']]);
        (new NotifyRangeLatestService)->saveMany($rangeList, ['notify_id', 'userid']);
        
        // 删除旧并添加新的添加督办信息
        (new InspectInfoService)->deleteAll(['type' => 1, 'relate_id' => $inputs['notify_id']]);
        $inspectAddData = [];
        $i = 0;
        foreach($inputs['inspectList'] ?? [] as $inspectInfo)
        {
            foreach($inspectInfo['inspectUserList'] as $inspectUserInfo)
            {
                if(!empty($inspectUserInfo)) {
                    $inspectUnitInfo = $inspectInfo['unitInfo'];
                    $inspectRangeIdList = ArrayHelper::getColumn($inspectInfo['rangeUserIdList'], 'user_id');
                    
                    $inspectAddData[$i]['inspect_user_id'] = (string)$inspectUserInfo['user_id'];
                    $inspectAddData[$i]['publish_user_id'] = (string)$userId;
                    $inspectAddData[$i]['unit_id'] = $inspectUnitInfo['id'];
                    $inspectAddData[$i]['range_id_json'] = json_encode($inspectRangeIdList);
                    $inspectAddData[$i]['type'] = 1; // 1为通知 2为征集
                    $inspectAddData[$i]['relate_id'] = $inputs['notify_id'];
                    $inspectAddData[$i]['inspect_type'] = $inputs['inspectType'];
                    $inspectAddData[$i]['create_timestamp'] = $updateTimestamp;
                    
                    $i ++;
                }
            }
        }
        (new InspectInfoService)->addMany($inspectAddData);
        
        (new RemindInfoService)->updateAll(['status' => 0], ['relate_id' => $inputs['notify_id'], 'type'=>1]);
        $addRemind = [];
        foreach($inputs['remind_times'] as $key =>$r_time){
                $addRemind[$key]['type'] = 1;
                $addRemind[$key]['relate_id'] = $inputs['notify_id'];
                $addRemind[$key]['timestamp'] = $r_time;
                $addRemind[$key]['has_send'] = 0;   
                $addRemind[$key]['content'] = ($inputs['remind_contents'][$key] == '') ? $inputs['title'] : $inputs['remind_contents'][$key];
                $addRemind[$key]['is_remind_me'] = $inputs['is_remind_me'][$key];
                $addRemind[$key]['is_remind_ontime'] = $inputs['is_remind_ontime'][$key];
        }
        (new RemindInfoService)->addMany($addRemind);

        //完成所有工作，修改进度
        (new NotifyService)->update(['process'=>2],['id' => $inputs['notify_id']]);    //阅读中
        (new NotifyLatestService)->update(['process'=>2],['id' => $inputs['notify_id']]);
    }
}